﻿using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Forms;
using SageKing.Blazor.OpenIdService.Components.Base;
using SageKing.Database.Contracts;
using SageKing.OpenIddict.Services.BaseService;

namespace SageKing.Blazor.OpenIdService.Components.Application.Pages.Manage.Components;

public partial class ApplicationEdit
{

    #region 参数

    List<string> _AllPermissions;

    #endregion

    #region 注入

    [Inject]
    public ApplicationApiHttpClient applicationApiService { get; set; }
    [Inject]
    public ApplicationApiBaseService _dataService { get; set; }
    #endregion

    #region IAddEdit 

    public override IBaseService<ApplicationViewModel, string> DataService
    { get { return _dataService; } }
    #endregion


    protected override async Task OnInitializedAsync()
    {
        try
        {
            await base.OnInitializedAsync();

            var getOptions = await applicationApiService.GetOptionsAsync();
            if (getOptions != null)
            {
                _AllPermissions = getOptions.Permissions;
            }

        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }

    #region 功能方法
    private Task onGenSecret(MouseEventArgs args)
    {
        model?.ClientSecret = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
        _ = this.Message?.SuccessAsync("已生成新的密钥，请妥善保管");
        return Task.CompletedTask;
    }

    private async Task onCopy(MouseEventArgs args)
    {
        try
        {

            if (model == null || model.ClientSecret.IsNullOrEmpty())
            {
                _ = this.Message?.ErrorAsync("没有可复制的内容");
                return;
            }
            await this.Clipboard(copyRef, model.ClientSecret);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

    #endregion

    #region 权限

    private void OnSelectedPermissionsChangedHandler(IEnumerable<string> values)
    {
        if (values != null)
            Console.WriteLine($"selected: ${string.Join(",", values)}");
    }
    #endregion 
}
